//
//  NSString+YY.m
//  ToolLibrary
//
//  Created by 蓓蕾 on 2024/1/8.
//  Copyright © 2024 Yang. All rights reserved.
//

#import "NSString+YY.h"

@implementation NSString (YY)
#pragma mark - 将字符串切割成数组【类方法】
+(NSArray *)slicingString:(NSString *)slicingString {
    NSMutableArray *tempArr = [NSMutableArray new];
    if ([NSString isNotBlankString:slicingString]) {
        NSString *currentString = [slicingString substringToIndex:1];
        [tempArr addObject:currentString];
        NSString *resultString = [slicingString substringFromIndex:1];
        while (resultString.length > 0) {
            NSString *currentString = [resultString substringToIndex:1];
            [tempArr addObject:currentString];
            resultString = [resultString substringFromIndex:1];
        }
    }
    
    return [NSArray arrayWithArray:tempArr];
}
#pragma mark - 将字符串切割成数组【实例方法】
-(NSArray *)slicingString {
    NSMutableArray *tempArr = [NSMutableArray new];
    if ([self isNotBlank]) {
        NSString *currentString = [self substringToIndex:1];
        [tempArr addObject:currentString];
        NSString *resultString = [self substringFromIndex:1];
        while (resultString.length > 0) {
            NSString *currentString = [resultString substringToIndex:1];
            [tempArr addObject:currentString];
            resultString = [resultString substringFromIndex:1];
        }
    }
    
    return [NSArray arrayWithArray:tempArr];
}

/// 获取文本宽度
- (CGFloat)yy_maxWidthWithFont:(UIFont*)font Height:(CGFloat)height Alignment:(NSTextAlignment)alignment LinebreakMode:(NSLineBreakMode)linebreakMode LineSpace:(CGFloat)lineSpace{
    return [self yy_sizeWithFont:font Size:CGSizeMake(CGFLOAT_MAX, height) Alignment:alignment LinebreakMode:linebreakMode LineSpace:lineSpace].width;
}

- (CGFloat)yy_maxWidthWithFont:(UIFont*)font Height:(CGFloat)height
{
    return [self yy_sizeWithFont:font Size:CGSizeMake(CGFLOAT_MAX, height) Alignment:NSTextAlignmentLeft LinebreakMode:NSLineBreakByWordWrapping LineSpace:0].width;
}

- (CGFloat)yy_maxHeightWithFont:(UIFont*)font Width:(CGFloat)width
{
    return [self yy_sizeWithFont:font Size:CGSizeMake(width, CGFLOAT_MAX) Alignment:NSTextAlignmentLeft LinebreakMode:NSLineBreakByWordWrapping LineSpace:0].height;
}

/// 获取文本高度
- (CGFloat)yy_maxHeightWithFont:(UIFont*)font Width:(CGFloat)width Alignment:(NSTextAlignment)alignment LinebreakMode:(NSLineBreakMode)linebreakMode LineSpace:(CGFloat)lineSpace{
    return [self yy_sizeWithFont:font Size:CGSizeMake(width, CGFLOAT_MAX) Alignment:alignment LinebreakMode:linebreakMode LineSpace:lineSpace].height;
}
- (CGSize)yy_sizeWithFont:(UIFont*)font Size:(CGSize)size Alignment:(NSTextAlignment)alignment LinebreakMode:(NSLineBreakMode)linebreakMode LineSpace:(CGFloat)lineSpace{
    if ([NSString isBlankString:self])
    {
        return CGSizeZero;
    }
    if (!font) {
        NSLog(@"字体大小有问题，请排查");
        font = [UIFont systemFontOfSize:15];
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = linebreakMode;
    paragraphStyle.alignment = alignment;
    if (lineSpace > 0) paragraphStyle.lineSpacing = lineSpace;
    NSDictionary *attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle};
    CGSize newSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:NULL].size;
    return CGSizeMake(ceil(newSize.width), ceil(newSize.height));
}

- (CGFloat)yy_maxHeightWithFont:(UIFont*)font Width:(CGFloat)width ParagraphStyle:(NSParagraphStyle *)paragraphStyle
{
    if ([NSString isBlankString:self]) {
        return 0;
    }
    
    NSDictionary *attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle};
    CGSize newSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:NULL].size;
    return ceil(newSize.height);
}

+(BOOL)isBlankString:(NSString *)string{
    if ([string isKindOfClass:[NSString class]] &&
        ![string isEqualToString:@"<null>"] &&
        ![string isEqualToString:@"(null)"] &&
        ![string isEqualToString:@"<NULL>"] &&
        ![string isEqualToString:@"(NULL)"] &&
        ![string isEqualToString:@"null"] &&
        ![string isEqualToString:@"NULL"] &&
        string.length > 0) {
        return NO;
    } else {
        return YES;
    }
}
-(BOOL)isBlank{
    if ([self isKindOfClass:[NSString class]] &&
        ![self isEqualToString:@"<null>"] &&
        ![self isEqualToString:@"(null)"] &&
        ![self isEqualToString:@"<NULL>"] &&
        ![self isEqualToString:@"(NULL)"] &&
        ![self isEqualToString:@"null"] &&
        ![self isEqualToString:@"NULL"] &&
        self.length > 0) {
        return NO;
    } else {
        return YES;
    }
}

+(BOOL)isNotBlankString:(NSString *)string{
    return ![self isBlankString:string];
}
-(BOOL)isNotBlank{
    return ![self isBlank];
}

@end
